﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameNesSL.Controls
{
    public partial class imagepulse : UserControl
    {
        public static DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(imagepulse), new PropertyMetadata(SourceChanged));

        private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((imagepulse) d).pulseImage.Source = (ImageSource) e.NewValue;
        }

        public static DependencyProperty PulseCountProperty = DependencyProperty.Register("PulseCount",typeof(int), typeof(imagepulse), null); 
        public int PulseCount
        {
            get
            {
                return (int) GetValue(PulseCountProperty);
            }
            set
            {
                SetValue(PulseCountProperty, value);
            }
        }



        public ImageSource Source
        {
            get
            {
                return (ImageSource) GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }

        private bool running = false;

        public void Start()
        {
            if (!running)
            {
                Visibility = System.Windows.Visibility.Visible;
                pulsed = 1;
                running = true;
                mouseIn.Begin();
            }
        }

        private int pulsed = 0;

        public static DependencyProperty StayVisibleProperty = DependencyProperty.Register("StayVisible",typeof(bool), typeof(imagepulse), new PropertyMetadata(false)); 
        public bool StayVisible
        {
            get
            {
                return (bool) GetValue(StayVisibleProperty);
            }
            set
            {
                SetValue(StayVisibleProperty, value);
            }
        }

        public void Stop()
        {
            Stop(false);
        }

        public void Stop(bool stayVisible)
        {
            if (running)
            {
                running = false;
                mouseOut.Stop();
                mouseIn.Stop();
            }
            if (!stayVisible)
            {
                Visibility = System.Windows.Visibility.Collapsed;
            }
        }


        public imagepulse()
        {
            InitializeComponent();
            mouseIn.Completed += new EventHandler(mouseIn_Completed);
            mouseOut.Completed += new EventHandler(mouseOut_Completed);
        }

        void mouseOut_Completed(object sender, EventArgs e)
        {
            if (running)
            {

                pulsed++;
                if (PulseCount == 0 || PulseCount >= pulsed)
                {
                    mouseIn.Begin();
                }
                else
                {
                    Stop(StayVisible);
                }
            }
        }

        void mouseIn_Completed(object sender, EventArgs e)
        {
            if (running)
            {
                mouseOut.Begin();
            }
        }
    }
}
